<template>
  <div>
    <div>
      <el-page-header @back="$router.go(-1)" content="发表页面">
      </el-page-header>
    </div>
    <div class="form" style="margin-top: 15px;">
      <el-form ::model="form">
        <el-row>
          <el-col :span="16">
            <el-form-item label="标题" :label-width="formLabelWidth">
              <el-input v-model="form.title"></el-input>
            </el-form-item>
            <el-form-item label="分类" :label-width="formLabelWidth">
              <el-select clearable v-model="form.categoryId" placeholder="请选择栏目">
                <el-option v-for="item in categories" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="封皮" :label-width="formLabelWidth">
              <el-upload class="avatar-uploader" action="http://121.4.83.32:7006/UploadControlle/upload"
                :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="正文" :label-width="formLabelWidth">
          <Tinymce v-model="form.content" :height="300"></Tinymce>
        </el-form-item>
      </el-form>
    </div>

    <!-- 返回按钮和发布按钮 -->
    <div class="button" style="text-align: right;">
      <el-link type="primary" :underline="false">返回</el-link>
      <el-button type="primary" style="margin-left: 20px;" @click="saveOrUpdate">发布</el-button>
    </div>
  </div>
</template>

<script>
// 辅助函数
import { mapGetters } from 'vuex'
// 引入富文本编辑器 
import Tinymce from '@/components/Tinymce'
import { title } from '@/settings';
import { get, post } from '@/utils/request'

export default {
  computed: {
    ...mapGetters([
      'userId',
    ])
  },
  // 局部注册组件 
  components: {
    Tinymce
  },
  data() {
    return {
      // 表单对象
      form: {},
      // 表单标签域宽度
      formLabelWidth: "60px",
      imageUrl: '',
      categories: []
    }
  },
  methods: {
    // 发布和修改文章
    async saveOrUpdate() {
      this.form.authorId = this.userId;
      let res = await post('/article/saveOrUpdate', this.form)
      if (res.status === 200) {
        this.$notify({
          title: '成功',
          message: 'res.message',
          type: 'success'
        })
        // 发布成功跳转到资讯列表页面
        this.$router.push({
          path: '/consult/list'
        })
      } else {
        this.$notify.error({
          title: '失败',
          message: 'res.message',
          type: 'info'
        })
      }
    },
    // 获取所有栏目
    async getCategories() {
      let res = await get('/category/findAll');
      this.categories = res.data;
    },
    // 图片上传成功的回调
    handleAvatarSuccess(res, file) {
      // console.log(res);
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.cover = res.data
    },
    // 上传之前的回调
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },

  },
  created() {
    this.getCategories(),
      this.form = { ...this.$route.query }
    this.imageUrl = this.form.cover
  }
}
</script>
